package rollercoaster.railway.tests;



import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import javax.swing.JFrame;

import rollercoaster.commons.Vertex3D;
import rollercoaster.commons.interfaces.ICurve3D;
import rollercoaster.railway.Circle;
import rollercoaster.railway.RoundedRectangle;
import rollercoaster.railway.UniformSecondOrderClosedBSpline;
import rollercoaster.railway.utils.OBJFileVertexReader;

import com.jogamp.opengl.util.FPSAnimator;


public class Curve3DTest extends JFrame {
	
	
	
	public static void main(String[] args) {
		Curve3DTest frame=new Curve3DTest();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public Curve3DTest() {
		setSize(1200,1200);
		setTitle("Beautiful Carousel");
		GraphicListener listener=new GraphicListener();
		GLCanvas canvas=new GLCanvas();
		canvas.addGLEventListener(listener);
		getContentPane().add(canvas);
		canvas.addKeyListener(listener);
		addKeyListener(listener);
		FPSAnimator animator=new FPSAnimator(canvas,60);
		animator.start();
	}
	
	
	public static class GraphicListener implements GLEventListener,KeyListener{

		private static final float H=0.5f;
		private static final float R=0.5f;
		private static final float h=0.05f;
		private static final float H1=0.3f;
		private static final int num_pali=8;
		
		private int view_angle=-30;
		private static float pulse=2;
		private static float pulse_sphere=5;
		private int t=0;
		
		private int texture1;
		private int texture2;
		private int texture3;
		private int texture4;
		private int texture5;
		
		private float wx=1,hx=1;
		
		public void display(GLAutoDrawable arg0) {
			GL2 gl=(GL2) arg0.getGL();
			gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
			
			
			gl.glEnable(GL.GL_DEPTH_TEST);
			//gl.glCullFace(gl.GL_FRONT);
			//gl.glEnable(gl.GL_CULL_FACE);
			//gl.glFrontFace(gl.GL_CCW);
			gl.glDisable(gl.GL_CULL_FACE);
			
			gl.glMatrixMode(GL2.GL_PROJECTION);
			gl.glLoadIdentity();
			gl.glRotatef(view_angle, 1, 0, 0);
			gl.glScalef(1200/wx, 1200/hx, 1);

			
			
			gl.glMatrixMode(GL2.GL_MODELVIEW);
			gl.glLoadIdentity();
			gl.glScalef(0.025f, 0.025f, 0.025f);
			//gl.glTranslatef(0, -0.5f, 0);
			
			//OBJFileVertexReader vr=new OBJFileVertexReader("obbijey/base.obj");
			//List<Vertex3D> vvs=vr.getVertices();
			//drawCurve(new UniformSecondOrderClosedBSpline(vvs), gl);
			
			OBJFileVertexReader vr2=new OBJFileVertexReader("obbijey/base_complex6.objxxx");
			List<Vertex3D> vvs2=vr2.getVertices();
			drawCurve(new UniformSecondOrderClosedBSpline(vvs2), gl);
			 // drawCurve(new RoundedSquare(5f,0.5f), gl);
			
			/*
			float ray=0.2f;
			float raylat=0.1f;
			float raylittle=0.05f;
			float x=(float) (0.8f*Math.cos(Math.PI/6));
			
			RoundedRectangle square=new RoundedRectangle(2*ray, 2*ray, ray/4);
			drawCurve(square, gl);
			RoundedRectangle lat1=new RoundedRectangle(2*raylat, 2*raylat, raylat/4);
			gl.glTranslatef(-x, ray/2, 0);
			drawCurve(lat1, gl);
			RoundedRectangle lat2=new RoundedRectangle(2*raylat, 2*raylat, raylat/4);
			gl.glLoadIdentity();
			gl.glTranslatef(x, ray/2, 0);
			drawCurve(lat2, gl);
			RoundedRectangle swish1=new RoundedRectangle(x*2, 2*raylittle, raylittle/4);
			gl.glLoadIdentity();
			gl.glTranslatef(0, ray/2, 0);
			drawCurve(swish1, gl);
			gl.glScalef(0.2f, 0.2f, 0.2f);
			
			t++;
			*/
		}

		public void drawCurve(ICurve3D curve, GL2 gl){
			gl.glLineWidth(2);
			gl.glShadeModel(GL2.GL_FLAT);
			gl.glColor3f(1, 1, 0);
			gl.glBegin(GL.GL_LINE_STRIP);
				float x=0f;
				for (int i = 0; i <= 100*(int)curve.getEndPoint(); i+=1) {
					if((int)i%2==0){
						gl.glColor3f(1, 0, 0);
					} else {
						gl.glColor3f(1, 0, 0);
					}
					Vertex3D v=curve.gamma(x);
					gl.glVertex3f(v.getX(), v.getY(), v.getZ());
					if(t==0){
						//System.out.println(v.getX()+" "+ v.getY()+" "+ v.getZ());
					}
					x+=0.04f;
				}
				
			gl.glEnd();
		}
		

		public void dispose(GLAutoDrawable arg0) {
			// TODO Auto-generated method stub
			
		}

	
		public void init(GLAutoDrawable arg0) {
			GL2 gl=(GL2) arg0.getGL();
			gl.glEnable(GL2.GL_LIGHTING);
			
			
			float ambient[]={0.5f,0.5f,0.5f,1};
			gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, ambient, 0);
			
			
			
			gl.glEnable(GL2.GL_COLOR_MATERIAL);
			gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
			float specColor[]={1,1,1,1};
			gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, specColor, 0);
			gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 80);
			
			
			
		}

		public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
				int arg4) {
			GL gl=arg0.getGL();
			
			wx=arg3;
			hx=arg4;
			
		}

		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}


		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_UP){
				view_angle+=5;
			}
			if(e.getKeyCode()==KeyEvent.VK_DOWN){
				view_angle-=5;
			}
			
		}

		
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
}

